import { login } from '@/api/user'
import * as auth from '@/utils/auth'

import { getUserInfo, getAvater } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: auth.getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken (state, token) {
      state.value = token
      auth.setToken(token)
    },
    removeToken (state) {
      state.token = ''
      auth.removeToken()
    },
    setUserInfo (state, userinfo) {
      state.userInfo = userinfo
      console.log(userinfo)
    },
    removeUserInfo (state) {
      state.userInfo = {}
    }
  },
  actions: {
    // 登录
    async loginGetToken ({ commit }, formdata) {
      const token = await login(formdata)
      console.log(token)
      commit('setToken', token)
    },

    // 获取用户信息
    async getUserInformation ({ commit }) {
      const res = await getUserInfo()
      const userData = await getAvater(res.userId)
      commit('setUserInfo', { ...res, ...userData })
      // 调用方法后并返回角色
      return res.roles
    }
  }
}
